<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class OrderController extends Controller
{
    //显示订单列表模板
    public function adminOrder(){
		//查询数据
		$result=DB::table('order')
		->join('goods','goods.gid','=','order.gid')
		->join('userdetail','userdetail.did','=','order.did')
		->join('getaddr','getaddr.id','=','order.aid')
		->select('order.*','dname','name','phone','location')
		->get();
		//分配数据
		return view('order.adminOrder',['result'=>$result]);
	}
	
	//修改订单状态
	public function aupdateOrder($id,$ostatus){
		//dd($ostatus);
		//更新数据
		if($ostatus){
			$result=DB::table('order')->where('id','=',$id)->update(['ostatus'=>0]);
		}else{
			$result=DB::table('order')->where('id','=',$id)->update(['ostatus'=>1]);
		}
		
		//判断结果
		if($result){
			return redirect('/order/adminOrder');
		}else{
			return redirect('/order/adminOrder');
		}
	}
	
	//订单删除
	public function delelteOrder($id){
		//删除数据
		
		$result=DB::table('order')->where('id','=',$id)->delete();
	
		if($result){
			return redirect('/order/adminOrder');
		}else{
			return redirect('/order/adminOrder');
		}
	}
	
	
	//订单修改
	public function updateOrder($id){
		//查询数据
		$res=DB::table('order')
		->join('getaddr','getaddr.id','=','order.aid')
		->select('order.*','name','phone','address','uid','location')
		->first();
		$uid=$res->uid;		
		$res1=DB::table('userdetail')->where('did','=',$uid)->first();
		$res->dname=$res1->dname;
		//dd($res);
		return view('order.updateOrder',['result'=>$res]);
	}
	
	//订单修改操作
	public function updateOrdera(Request $request){
		//接受表单传递过来的值
		$result=$request->only('onum');
		$result1=$request->only('name','location','phone');
		$id=$request->only('id');
		$uid=$request->only('uid');
		//插入数据	
		$res=DB::table('order')->where('id','=',$id['id'])->update($result);
		$res1=DB::table('getaddr')->where('id','=',$uid['uid'])->update($result1);
		if($res||$res1){
			return redirect('/order/adminOrder');
		}else{
			return redirect("/order/updateOrder/{$id['id']}");
		}
	}
}
